/**
 * @author mars
 */

$(function(){
	profile.init();
});

var profile = {
	
	cons : {
		num_top_nebula 		: 	4,
		num_top_post 		: 	5,
		num_max_tag 		: 	16,
		num_max_recent_posts:   6,
		num_max_recent_galaxies:   6
	},
	
	init: function(){
        render_top_bar();
		//invite msg
		
		var msgBlock = $('.msg-block');
		/*
		msgBlock.find($('.content')).click(function(){
			invite_msg_callback(msgBlock);
		});
		*/
        var user_avatar = profile.render('user-avatar',session_user_info);
        $('.avatar').html(user_avatar);
        //resize user avatar on the lop right
        var userAvatar = $('.avatar .avatar-area img');
        reSizeAvatar(userAvatar);
		var user_info_block = $('#profile-container .status-block');
		get_user_info(user_info_block);
		var top_nebula_block = $('#news-container .nebulas-block');
		get_top_nebulas(profile.cons.num_top_nebula, top_nebula_block);
		var top_posts_block = $('#news-container .news-block ul');
		get_top_posts(profile.cons.num_top_post, top_posts_block);
		var tagged_nebula = $('#profile-container .tagged-nebula');
		get_tagged_nebulas(profile.cons.num_max_tag, tagged_nebula, session_user_info['uid'],true);
		get_recent_posts( profile.cons.num_max_recent_posts, $('#recent-posts'),session_user_info['uid'],true);
		get_recent_galaxies( profile.cons.num_max_recent_galaxies, $('#recent-galaxies'),session_user_info['uid'],true );
	},

	render:function(template, params){
		var arr = [];
		switch(template){
			case 'invit-msg':
				arr = [
					
				];
				break;
            case 'user-avatar':
                arr = [
                    '<div style="padding: 2px; width:169px; height: 169px;" class="avatar-area">',
                    '<img style="display:none;" src="',params['avatar'],'"/></div>',
                    '<span style="text-align: center; margin: 5px 0 5px 0;">',params['username'],'</span>'
                ];
                break;
			case 'top-nebulas':
				var length = 120;
				var proc_string = params['description'];
				if(proc_string.length>length){
					proc_string = proc_string.substring(0, length);
					proc_string = proc_string + "...";
				}
				arr = [
					'<div class="nebula-record"><span class="icon"><a href="./topics.php?nid=',params['nid'],'"><img style="width: 100px; height: 100px;"',
					,'src="',params['icon'],
					'"/></a></span><div class="content"><span class="name"><a href="./topics.php?nid=',params['nid'],'">',params['name'],'</a>',
					'</span><div class="desc">',proc_string,'</div></div></div>'
							
				];
				break;
			case 'top-posts':
				var length = 100;
				var proc_string = htmlDecode(params['body']);
                //uncount the tag content
                length += extentPostLength(proc_string);
				if(proc_string.length>length){
					proc_string = proc_string.substring(0, length);
					proc_string = proc_string + "...";
				}
                //modify author's avatar
                if(params['author']['avatar']==0)
                    params['author']['avatar']= "./img/noavatar_small.gif";
				arr=[
					'<li><div class="left"><div class="avatar-area" style="height:50px;width:50px">',
                    '<img style="display:none" src="',params['author']['avatar'],'"/></div></div>',
					'<div class="right"><span class="title"><a href="./topics.php?nid=',params['default_nid'],'&pid=',params['pid'],'">',params['title'],'</span></a><span class="content">',proc_string,
					'</span><span class="end">posted by <a href="./circlefile.php?uid=',params['uid'],'">'
                    , params['author']['username'],'</a> ',params['ts'],'</span></div></li>'
				];
				break;	
		}
		return arr.join('');
	}
}

function invite_msg_callback(target){

	target.find($('.space-holder')).animate({
		opacity : 0
	}, {
		duration : 200,
		complete : function() {
			$(this).hide();
			target.find($('ul')).slideDown();
		}
	});


}

function get_top_nebulas(num, contentBlock){
	contentBlock.html('<div class="section-paddingL loading"><img src="img/loading.gif"/></div');
	$.ziGET('top_nebulas', {'n':num}, function(r) {
		var markup="";
		for(var i = 0; i < num; i++) {
			markup = markup + profile.render('top-nebulas', r[i]);
		}
		contentBlock.html(markup);
	});


}

function get_top_posts(num, contentBlock){
	contentBlock.html('<div class="section-paddingL loading"><img src="img/loading.gif"/></div>');
	$.ziGET('top_posts', {'n':num}, function(r) {
		var markup="";
		num = r.length < num ? r.length : num;
		for(var i = 0; i < num; i++) {
			var markup = markup+profile.render('top-posts', r[i]);
		}
		contentBlock.html(markup);
        var imgTag = $('.news-block .content img');
        if(imgTag.length>0){
            reSizeImg(imgTag);
        }
        var avatarTag = $('.news-block .avatar-area img');
        if(avatarTag.length>0)
            reSizeAvatar(avatarTag);
	});
}


